package logic

import (
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"net/http"
	"strconv"
	"tiktok/dao"
	"tiktok/models"
	"tiktok/web"
)

func (p dbinfor) TableName() string {
	return "TIKTOK_USER"
}

type informsg struct {
	models.Status
	models.User `json:"user"`
}
type dbinfor struct {
	models.User
	Token    string `gorm:"column:token" json:"token"`
	Password string `gorm:"column:password" json:"password"`
}

func Information(ctx *gin.Context) {
	uid := ctx.Query("user_id")
	token := ctx.Query("token")
	db := dao.GetDB(ctx)
	user := dbinfor{}
	reuid, err := strconv.ParseInt(uid, 10, 64)
	if err != nil {
		logrus.Error("string to int 64失败")
	}
	result := db.Where("uid=?", uid).Find(&user)
	if result.Error != nil {
		logrus.Error("注册查询信息失败")
		ctx.JSON(http.StatusBadRequest, informsg{
			Status: models.Status{
				StatusCode: -1,
				StatusMsg:  "注册返回信息失败",
			},
		})
		return
	}

	claim, err := web.ParseToken(token)
	if claim == nil {
		logrus.Error("token 过期", err)
		ctx.JSON(http.StatusBadRequest, informsg{
			Status: models.Status{
				StatusCode: -1,
				StatusMsg:  "",
			},
		})

		return
	}

	userms := models.User{
		Uid:           reuid,
		Name:          user.Name,
		FollowCount:   user.FollowCount,
		FollowerCount: user.FollowerCount,
		IsFollow:      true,
	}

	ctx.JSON(http.StatusOK, informsg{
		Status: models.Status{
			StatusCode: 0,
			StatusMsg:  "注册返回信息成功",
		},
		User: userms,
	})

}
